package cn.scw.project.facility.controller;

import cn.scw.common.constant.SysConsts;
import cn.scw.common.utils.SecurityUtils;
import cn.scw.framework.aspectj.lang.annotation.Log;
import cn.scw.framework.aspectj.lang.enums.BusinessType;
import cn.scw.framework.security.LoginUser;
import cn.scw.framework.web.controller.BaseController;
import cn.scw.framework.web.domain.AjaxResult;
import cn.scw.framework.web.page.TableDataInfo;
import cn.scw.project.facility.domain.FacPrintOrder;
import cn.scw.project.facility.domain.netework.bean.FacFacilityAddReq;
import cn.scw.project.facility.domain.netework.bean.FacFacilityListReq;
import cn.scw.project.facility.domain.vo.FacFacilityVo;
import cn.scw.project.facility.service.IFacFacilityService;
import cn.scw.project.facility.service.IFacPrintOrderService;
import cn.scw.project.separate.domain.FacSaInfo;
import cn.scw.project.separate.service.FacSaInfoService;
import cn.scw.project.system.domain.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 设备Controller
 *
 * @author api
 * @date 2020-05-20
 */
@Api(tags = "设备")
@RestController
@RequestMapping(SysConsts.CONTEXT_PATH + "/facility/facility")
public class FacFacilityController extends BaseController {

    @Autowired
    private IFacFacilityService facFacilityService;

    @Resource
    private FacSaInfoService facSaInfoService;

    @Autowired
    private IFacPrintOrderService iFacPrintOrderService;

    @ApiOperation("查询设备列表")
    @GetMapping("/list")
    public TableDataInfo list(FacFacilityListReq req) {
        startPage();
        List<FacFacilityVo> list = facFacilityService.selectFacFacilityList(req);
        return getDataTable(list);
    }

    @ApiOperation("查询分账成员")
    @GetMapping("/separate")
    public AjaxResult separate(String code) {
        List<FacSaInfo> longList = facSaInfoService.querySaId(code);
        return AjaxResult.success(longList);
    }

    @ApiOperation("获取设备详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        FacFacilityVo vo = facFacilityService.selectFacFacilityVoById(id);
        return AjaxResult.success(vo);
    }

    @ApiOperation("根据code获取设备详细信息")
    //@PreAuthorize("@ss.hasPermi('facility:facility:query')")
    @GetMapping(value = "/code/{code}")
    public AjaxResult getInfoByCode(@PathVariable String code) {
        return AjaxResult.success(facFacilityService.selectFacFacilityVoByCode(code));
    }

    @ApiOperation("根据code获取设备详细信息")
    //@PreAuthorize("@ss.hasPermi('facility:facility:query')")
    @GetMapping(value = "/wx/code/{code}")
    public AjaxResult getWxInfoByCode(@PathVariable String code) {
        return AjaxResult.success(facFacilityService.selectFacFacilityVoByCode(code));
    }

    @ApiOperation("新增设备")
    //@PreAuthorize("@ss.hasPermi('facility:facility:add')")
    @Log(title = "设备", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(FacFacilityAddReq req) {
        facFacilityService.insertFacFacility(req);

        if (req.getSeparates() != null && req.getSeparates().size() > 0) {
            req.getSeparates().forEach(saId -> {
                FacSaInfo facSaInfo = new FacSaInfo();
                facSaInfo.setScale(BigDecimal.ZERO);
                facSaInfo.setFacId(req.getCode());
                facSaInfo.setSaId(saId);
                facSaInfoService.save(facSaInfo);
            });
        }
        return AjaxResult.success();
    }

    @ApiOperation("新增分账用户")
    @Log(title = "新增分账用户", businessType = BusinessType.INSERT)
    @PostMapping("/separate/add")
    public AjaxResult addSeparate(@RequestBody FacSaInfo req) {
        if (req.getScale() == null) return AjaxResult.error("请输入分账比例");
        facSaInfoService.save(req);
        return AjaxResult.success();
    }

    @ApiOperation("删除分账用户")
    @Log(title = "删除分账用户", businessType = BusinessType.INSERT)
    @PostMapping("/separate/delete")
    public AjaxResult addDelete(@RequestBody FacSaInfo req) {
        facSaInfoService.deleteFacUser(req.getFacId(), req.getSaId());
        return AjaxResult.success();
    }

    @ApiOperation("修改设备")
    //@PreAuthorize("@ss.hasPermi('facility:facility:edit')")
    @Log(title = "设备", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(FacFacilityAddReq req) {

        facFacilityService.updateFacFacility(req);
        if (req.getSeparates() != null && req.getSeparates().size() > 0) {
            facSaInfoService.clear(req.getCode());
            req.getSeparates().forEach(saId -> {
                FacSaInfo facSaInfo = new FacSaInfo();
                facSaInfo.setFacId(req.getCode());
                facSaInfo.setSaId(saId);
                facSaInfo.setScale(BigDecimal.ZERO);
                facSaInfoService.save(facSaInfo);
            });
        }
        return AjaxResult.success();
    }

    @ApiOperation("删除设备")
    //@PreAuthorize("@ss.hasPermi('facility:facility:remove')")
    @Log(title = "设备", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(facFacilityService.deleteFacFacilityByIds(ids));
    }

    @ApiOperation("设备清零")
    @GetMapping("/facilityreset/{code}")
    @PutMapping
    public AjaxResult facilityreset(@PathVariable String code) {
        FacPrintOrder order = new FacPrintOrder();
        order.setFacilityCode(code);
        LoginUser loginUser= SecurityUtils.getLoginUser();
        if (loginUser!=null) {
            SysUser user = loginUser.getUser();
            if (user!=null)
                order.setResetUserId(user.getUserId());
        }
        order.setResetStatus(1);
        order.setResetDate(new Date());
        iFacPrintOrderService.updateFacPrintOrderReset(order);
        return AjaxResult.success();
    }

}
